﻿using System.Web;
using System.Web.Mvc;
using GKFX.Models.Common;
using GKFX.Common.Business.BOL.Common;
using GKFX.Common.Business.DataContexts;
using System.Linq;
using System.Collections.Generic;
using GKFX.Business.Repository;
using GKFX.Web.Security.Authorizations;
using GKFX.Web.Security;
using GKFX.Common.Structures;

namespace Century.EnterpriseLibrary.Globalization.MVC
{
    public static class GlobalizationExtensions
    {

        #region [ Label(s) ]

        public static IHtmlString Translate(this string defaultValue)
        {
            return Translate(defaultValue, defaultValue);
        }

        public static IHtmlString Translate(this string defaultValue, string key, string category = "")
        {
            string value = LabelValue(defaultValue, key, category);
            return new HtmlString(value);
        }

        public static IHtmlString Label(this string defaultValue)
        {
            return Label(defaultValue, defaultValue);
        }

        public static IHtmlString Label(this string defaultValue, string key, string category = "")
        {
            string value = LabelValue(defaultValue, key, category);
            
            GKFXAdminToken adminToken = null;
            if (HttpContext.Current != null && HttpContext.Current.Session["AdminToken"] != null)
            {
                adminToken = (GKFXAdminToken)HttpContext.Current.Session["AdminToken"];
            }
            
            if (
                    (adminToken != null && adminToken.AccessibleSites.Any(s => s.Id == PersistData.CurrentSite.Id)) &&
                    GKFXAuthorization.Authorize(HttpContext.Current.User.Identity.Name, new Permission { Name = "InlineEditing", DisplayName = "Inline Editing" }) &&
                    (HttpContext.Current.Request.Params["desingmode"] == null || HttpContext.Current.Request.Params["desingmode"] != "true")
                    
               )
            {
                value = string.Format("<far class='{3}' key='{0}' category='{1}'>{2}</far>"
                        , HttpUtility.HtmlEncode(key)
                        , HttpUtility.HtmlEncode(category)
                        , value
                        , "localizedText");
            }


            return new HtmlString(value);
        }

        private static string LabelValue(string defaultValue, string key, string category)
        {
            if (string.IsNullOrEmpty(key) && string.IsNullOrEmpty(category))
            {
                return defaultValue;
            }

            return Century.EnterpriseLibrary.Globalization.GlobalizationManager.CMSTranslate(category, key, defaultValue);
        }

        #endregion


        #region [ Position(s) ]

        public static MvcHtmlString Position(this HtmlHelper helper, string name)
        {
            bool isAuthorize = false;
            if (
                GKFXAuthorization.Authorize(HttpContext.Current.User.Identity.Name, new Permission { Name = "InlineEditing", DisplayName = "Inline Editing" }) &&
                (HttpContext.Current.Request.Params["draftMode"] != null && HttpContext.Current.Request.Params["draftMode"] == "true")
               )
            {
                isAuthorize = true;
            }
            ContentHolder contentHolder = PersistData.CurrentPage.PageLayout.ContentHolders.First(c => c.Name == name);
            List<ContentHolderPart> holderParts = PersistData.CurrentPage.ContentHolderParts.Where(c => c.ContentHolderId == contentHolder.Id).OrderBy(c => c.PartOrder).ToList();

            string result = string.Empty;
            if (isAuthorize)
            {
                // only admins can see addview button
                string addButtonsStr = "<a href='#' class='addContent'>Add Content</a>";
                if (GKFXAuthorization.Authorize(HttpContext.Current.User.Identity.Name, new Permission { Name = "Administration", DisplayName = "Administration" }))
                {
                    addButtonsStr = string.Format("{0}<a href='#' class='addView' >Add View</a>", addButtonsStr);
                }

                result = string.Format("<far class='{2}' key='{0}' pageId='{1}' holderId='{3}'>{4}", 
                                        name,
                                        PersistData.CurrentPage.Id, 
                                        "localizedPosition", 
                                        contentHolder.Id,
                                        addButtonsStr
                                      );
                foreach (var holderPart in holderParts)
                {

                    if (holderPart.StaticContentId.HasValue)
                    {
                        result +=
                      @"<p><div class='ContentHolderHeader'>
                            <img src='/content/themes/default/img/editor/c_move.jpg' width='10px' height='20px' class='positionContentHolderMove' alt='move'/>
                            <img src='/content/themes/default/img/editor/c_edit.gif' class='positionContentHolderEdit' alt='Edit'/>
                            <img src='/content/themes/default/img/editor/c_remove.gif' class='positionContentHolderRemove' alt='remove'/>
                            <img src='/content/themes/default/img/editor/c_up.gif' class='positionContentHolderToggle' alt='Toggle'/>
                        </div>
                        <div style='clear: both;' ></div>";
                        result += string.Format("<far class='{2}' key='{0}' pageId='{1}' ordid='{3}' holderId='{4}' partId='{5}'>", holderPart.StaticContentId.Value,
                            PersistData.CurrentPage.Id, "localizedStaticContent", holderPart.PartOrder, holderPart.ContentHolderId, holderPart.Id);
                        result += holderPart.StaticContent.Description;// +@"<br />";
                        result += "</far></p>";
                    }
                    else if (holderPart.DynamicContentId.HasValue)
                    {
                        result +=
                      @"<p><div class='ContentHolderHeader'>
                            <img src='/content/themes/default/img/editor/c_move.jpg' width='10px' height='20px' class='positionContentHolderMove' alt='move'/>
                            <img src='/content/themes/default/img/editor/c_remove.gif' class='positionContentHolderRemove' alt='remove'/>
                            <img src='/content/themes/default/img/editor/c_up.gif' class='positionContentHolderToggle' alt='Toggle'/>
                        </div>
                        <div style='clear: both;' ></div>";
                        result += string.Format("<far class='{2}' key='{0}' pageId='{1}' ordid='{3}' holderId='{4}' partId='{5}'>", holderPart.DynamicContentId.Value,
                            PersistData.CurrentPage.Id, "localizedDynamicContent", holderPart.PartOrder, holderPart.ContentHolderId, holderPart.Id);
                        result += System.Web.Mvc.Html.ChildActionExtensions.Action(helper, holderPart.DynamicContent.ActionName, holderPart.DynamicContent.ControllerName, new { area = holderPart.DynamicContent.AreaName });
                        result += "</far></p>";

                    }

                }
                result += "</far>";
            }
            else
            {
                foreach (var holderPart in holderParts)
                {
                    if (holderPart.StaticContentId.HasValue)
                    {
                        result += string.Format("<p>{0}</p>", holderPart.StaticContent.Description);
                    }
                    else if (holderPart.DynamicContentId.HasValue)
                    {
                        result += string.Format("<p>{0}</p>", System.Web.Mvc.Html.ChildActionExtensions.Action(helper, holderPart.DynamicContent.ActionName, holderPart.DynamicContent.ControllerName, new { area = holderPart.DynamicContent.AreaName }));
                    }

                }
            }
            return new MvcHtmlString(result);
        }

        #endregion

        #region [ Private Method(s) ]


        #endregion



    }
}
